package neu.competition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import neu.competition.dto.UserDTO;
import neu.competition.entity.User;
import neu.competition.mapper.UserMapper;
import neu.competition.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper um;
    @Autowired
    UserMapper userMapper;

    public boolean register(String userid, String email, String username, MultipartFile image, String occupation, String password) {
        if ("".equals(userid) || "".equals(email) || "".equals(username) || "".equals(password))
            return false;
        if (userid.length() > 12)
            return false;
        if (password.length() > 20)
            return false;
        if (username.length() > 20)
            return false;
        if (email.length() > 256)
            return false;
        QueryWrapper<User> qw = new QueryWrapper<User>();
        qw.eq("userId", userid);
        if (um.exists(qw))
            return false;
        User u = new User();
        u.setUserid(userid);
        u.setPassword(password);
        u.setEmail(email);
        u.setRealname(username);
        u.setUsertype(Integer.valueOf(occupation));
        u.setPhone("15534597515");
        u.setIntroductory("");
        try {
            byte[] b = image.getBytes();
            String UPLOADED_FOLDER = new File("").getCanonicalPath() + "\\src\\main\\resources\\static\\user\\user_images\\";
            Path p = Paths.get(UPLOADED_FOLDER + image.getOriginalFilename());
            Files.write(p, b);
            if (image.getOriginalFilename().length() > 256)
                return false;
            u.setImage(image.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return um.insert(u) > 0;
    }

    public void tomodify(User user, int usertype,HttpSession session, Model model) {
        System.out.println(user.toString());
        user.setUsertype(usertype);
        userMapper.updateById(user);
        QueryWrapper queryWrapper = new QueryWrapper();
        UserDTO users1 = (UserDTO) session.getAttribute("USER_INFO");

        User user2 = userMapper.selectById(users1.getUserid());


        model.addAttribute("realname", user.getRealname());
        model.addAttribute("introduce", user.getIntroductory());
        model.addAttribute("phone", user.getPhone());
        model.addAttribute("email", user.getEmail());
    }

    public void modify(HttpSession session, Model model) {
        QueryWrapper queryWrapper = new QueryWrapper();
        UserDTO users1 = (UserDTO) session.getAttribute("USER_INFO");
        System.out.println(users1.toString());
        User user2 = userMapper.selectById(users1.getUserid());
        System.out.println(user2.toString());

        model.addAttribute("realname", user2.getRealname());
        model.addAttribute("introduce", user2.getIntroductory());
        model.addAttribute("phone", user2.getPhone());
        model.addAttribute("email", user2.getEmail());
    }

    public UserDTO login(String userid, String password) {
        if ("".equals(userid) || "".equals(password))
            return null;
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("userid", userid);
        User u = um.selectOne(qw);
        if (u == null || !u.getPassword().equals(password))
            return null;
        UserDTO dto = new UserDTO();
        dto.setEmail(u.getEmail());
        dto.setPassword("***");
        dto.setRealname(u.getRealname());
        dto.setUserid(u.getUserid());
        dto.setUsertype(u.getUsertype());
        dto.setImage(u.getImage());
        dto.setIntroductory(u.getIntroductory());
        dto.setPhone(u.getPhone());
        System.out.println(dto.getImage());
        return dto;
    }

    public UserDTO getUserById(String uid) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("userid", uid);
        UserDTO dto = new UserDTO();
        BeanUtils.copyProperties(um.selectOne(qw), dto);
        return dto;
    }

}
